<%--
    Document   : index
    Created on : 12.02.2010, 22:48:51
    Author     : Morfant
--%>

<%@page import="stud.cp.user.valueobjects.DBObject"%>
<%@page import="stud.cp.user.valueobjects.DBObjectType"%>
<%@page import="java.util.List"%>
<%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <%@include file="/header.jsp" %>
    <script type="text/javascript">     
        function cancelEvent(e){
            if (!e)
                if (window.event) e = window.event;
            else return;
            if (e.cancelBubble != null) e.cancelBubble = true;
            if (e.stopPropagation) e.stopPropagation();
            if (e.preventDefault) e.preventDefault();
            if (window.event) e.returnValue = false;
            if (e.cancel != null) e.cancel = true;
        }
        function isActionCanBePerform(obj,e){
            var inputs = document.getElementsByTagName("input");
            var i=0;
            var isAtLeastOneCheckBoxSelect=false;
            while((i<inputs.length)&&(!isAtLeastOneCheckBoxSelect)){
                if(inputs[i].type=="checkbox"){
                    if(inputs[i].checked){
                        isAtLeastOneCheckBoxSelect=true;
                    }
                }
                i++;
            }
            if(!isAtLeastOneCheckBoxSelect){
                alert("Выберите хотя бы один объект!");
                cancelEvent(e);
            }else{
                obj.href=obj.href;
            }
        }
        function checkOne(obj)
        {
            var grid = obj.parentNode.parentNode.parentNode;
            var inputs = grid.getElementsByTagName("input");
            for(var i=0;i<inputs.length;i++)
            {
                if (inputs[i].type =="checkbox")
                {
                    if(obj.checked && inputs[i] != obj && inputs[i].checked)
                    {
                        inputs[i].checked = false;
                    }
                }
            }
        }
    </script>

    <body>
        <div class="page">
            <%@include file="/cap.jsp" %>
            <div id="main">
                <%--@include file="/Designer/designer_cap.jsp" --%>
                <% DBObjectType objectType = (DBObjectType) request.getAttribute(DesignerPluginConst.PARENT_OBJECT_TYPE);%>
                <h2><%=objectType.getName()%>. Список продуктов заданного типа </h2>
                <table id="buttontable">
                    <tr>
                        <th>
                            <div id="objectmenucontainer">
                                <ul id="objectmenu">
                                    <li><a href="<%= response.encodeURL("designer-action.do")
                                            + "?"
                                            + PluginConst.SUBACTION
                                            + "="
                                            + PluginConst.GET_ADD_PAGE%>">Добавить продукт</a></li>
                                </ul>
                            </div>
                        </th>
                        <th>
                            <div id="objectmenucontainer">
                                <ul id="objectmenu">
                                    <li><a href="<%= response.encodeURL("designer-action.do")
                                            + "?"
                                            + PluginConst.SUBACTION
                                            + "="
                                            + PluginConst.GET_EDIT_PAGE%>" onclick="isActionCanBePerform(this,event)">Редактировать продукт</a></li>
                                </ul>
                            </div>
                        </th>
                        <th>
                            <div id="objectmenucontainer">
                                <ul id="objectmenu">
                                    <li><a href="<%= response.encodeURL("designer-action.do")
                                            + "?"
                                            + PluginConst.SUBACTION
                                            + "="
                                            + PluginConst.GET_DELETE_PAGE%>" onclick="isActionCanBePerform(this,event)">Удалить продукт</a></li>
                                </ul>
                            </div>
                        </th>
                    </tr>

                </table>
                <% List<DBObject> list =
                            (List<DBObject>) request.getAttribute(DesignerPluginConst.OBJECT_HEIRS_LIST);
                    if ((list != null) && (list.size() > 0)) {%>
                <table>
                    <tr>
                        <th>
                        </th>
                        <th>
                            Продукт
                        </th>
                        <th>
                            Описание
                        </th>
                    </tr>
                    <%for (DBObject item : list) {%>
                    <tr>
                        <td>
                            <input type="checkbox"
                                   name="<%=PluginConst.OBJECT_ID %>"
                                   value="<%=item.getObject_type_id()%>"
                                   onclick="checkOne(this)"/>
                        </td>
                        <td>
                            <%=item.getName()%>
                        </td>
                        <td>
                            <textarea readonly
                                      rows="3"
                                      cols="40"
                                      name="show"
                                      ><%=item.getDescription()%></textarea>
                        </td>
                    </tr>
                    <% }%>
                </table>
                <% }%>

                <%if (objectType.getParentId() != null) {%>
                <div id="objectmenucontainer">
                    <ul id="objectmenu">
                        <li><a href="<%= response.encodeURL("designer-action.do")
                                + "?"
                                + PluginConst.SUBACTION
                                + "="
                                + DesignerPluginConst.GET_OBJECT_TYPE_HEIRS
                                + "&"
                                + PluginConst.OBJECT_TYPE_ID
                                + "="
                                + objectType.getParentId()%>">Вернуться к предыдущему уровню иерархии продуктов</a></li>
                    </ul> 
                </div>
                <%}%>
            </div>
        </div>
    </body>
</html>
